﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class CMDUtil
    {
        /// <summary>
        /// 使用运行，调用其他程序
        /// </summary>
        /// <param name="argument">命令参数</param>
        /// <param name="applocaltion">应用名称</param>
        /// <returns></returns>
        public static string ExecuteOutCmd(string argument, string applocaltion)
        {
            using (Process process = new Process())
            {
                process.StartInfo.Arguments = argument;
                process.StartInfo.FileName =applocaltion;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                //process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;

                process.Start();            

                //获取cmd窗口的输出信息  
                string output = process.StandardOutput.ReadToEnd();

                process.WaitForExit();
                process.Close();

                return output;
            }
        }
    }
    public sealed class ShellCmd
    {
        /// <summary>
        /// 事件可能被多处绑定 之后再来解决这个问题，比如用key_Value的形式解决
        /// </summary>
        /// <param name="cmdResult"></param>
        /// <returns></returns>
        public delegate void CmdResultEventHandler(string cmdResult);
        public event CmdResultEventHandler cmdResultEvent;

        private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                string cmdResult = e.Data;
                //Debug.WriteLine(cmdResult);
                if (cmdResultEvent != null)
                {
                    cmdResultEvent(cmdResult);
                }
            }
        }

        private void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                string cmdResult = e.Data;
                Debug.WriteLine(cmdResult);
            }
        }

        /// <summary>
        /// 异步执行方式
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="paramsStr"></param>
        public void AsyncCmdExcute(string filename, string paramsStr)
        {
            using (Process process = new System.Diagnostics.Process())
            {
                process.StartInfo.FileName = filename;
                process.StartInfo.Arguments = paramsStr;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;
                //process.StartInfo.RedirectStandardError = true;  // 重定向错误输出
                process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
                process.Start();
                process.BeginOutputReadLine();
                // process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);  
            }
        }
        /// <summary>
        /// 同步执行方式
        /// </summary>
        /// <param name="paramsStr"></param>
        public static string SyncCmdExcute(string paramsStr)
        {
            string adbFilePath = @"D:\Program Files (x86)\AnyTrans\AnyTrans\adb.exe";
            string errorCode = "";
            string output = "";
            try
            {
                Process cmd = new Process();
                cmd.StartInfo.FileName = adbFilePath;
                cmd.StartInfo.Arguments = paramsStr;
                cmd.StartInfo.UseShellExecute = false;
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.RedirectStandardError = true;
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.CreateNoWindow = false;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                cmd.Start();
                output = cmd.StandardOutput.ReadToEnd();
                errorCode = cmd.StandardError.ReadToEnd();
                if (string.IsNullOrEmpty(errorCode))
                {
                    errorCode = output;
                }
                cmd.WaitForExit();
                cmd.Close();
            }
            catch (Exception ex)
            {
                //ToolsHelper.SysLog.LogMessager.WriteInfo("Start adb Exception:" + ex.Message);
                Console.WriteLine("Start adb Exception:" + ex.Message);
            }
            return output;
        }
    }
}
